// components/chooseMedia/chooseMedia.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    zwImg: {
      //提交图片的默认图片
      type:String,
      value:'/images/add.png'
    },
    bank_img: {
      //上传的图片路径
      type: String,
      value: ''
    },
    isShow: {
      type: Boolean,
      value: false
    }
  },

  /**
   * 组件的初始数据
   */
  data: {

  },

  /**
   * 组件的方法列表
   */
  methods: {
    //选择图片
    chooseMedia() {
      wx.chooseMedia({
        count: 1,
        mediaType: ['image'],
        sourceType: ['album', 'camera'],
        maxDuration: 30,
        camera: 'back',
        success: (res) => {
          let tempFilePath = res.tempFiles[0].tempFilePath;
          this.uploadFile(tempFilePath)
        }
      })
    },

    //将图片上传到服务器
    uploadFile(tempFilePath) {
      wx.showLoading();
      wx.uploadFile({
        filePath: tempFilePath,
        name: 'file',
        url: 'https://199shq.com//api/common/upload',    //配置自己的上传图片的url地址
        formData: {
          'data': 'bank_img'
        },
        success: (res) => {
          console.log(JSON.parse(res.data).data);
          wx.hideLoading();
          const bank_img = JSON.parse(res.data).data; //将数据字符串变成对象
          if (bank_img) {
            this.setData({
              bank_img: bank_img,
              isShow: true
            })
            getApp().globalData.bank_img = bank_img;
          }
          this.triggerEvent('chooseMedia', bank_img)
        }
      })
    },

    //删除图片
    closeBth(){
      this.setData({
        isShow: false,
        bank_img: ''
      })
      //执行了删除按钮，清空了图片
      console.log("执行了清空按钮");
      console.log("图片名称：" + this.data.bank_img);
      getApp().globalData.bank_img = this.data.bank_img;
    }
  }
})
